package org.pjlib.widget;

import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.coorchice.library.SuperTextView;

import org.pjlib.R;
import org.pjlib.common.ImgUtils;

/**
 * Created by ws on 2019/04/03.
 */
public class MenuItem extends LinearLayout implements Menu {
    private TextView title;
    public ImageView pic;
    private int selRes;
    private int noSelRes;
    private int norColor;
    private int selColor;
    private String text;
    private View placeView;
    public boolean isAnim=true;
    private SuperTextView red;
    public MenuItem(Context context) {
        this(context, null);
    }

    public MenuItem(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public MenuItem(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        setOrientation(VERTICAL);
        View view = LayoutInflater.from(context).inflate(R.layout.menu_item, null);
        LayoutParams params = (LayoutParams) view.getLayoutParams();
        if (params == null) {
            params = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
            view.setLayoutParams(params);
        } else {
            params.height = LayoutParams.MATCH_PARENT;
            params.width = LayoutParams.MATCH_PARENT;
        }
        addView(view);
        setBackgroundColor(Color.TRANSPARENT);
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.MenuItem, defStyleAttr, 0);
        text = a.getString(R.styleable.MenuItem_menu_text);
        selRes = a.getResourceId(R.styleable.MenuItem_menu_sel_pic, 0);
        noSelRes = a.getResourceId(R.styleable.MenuItem_menu_nor_pic, 0);
        norColor = a.getColor(R.styleable.MenuItem_menu_text_nor_color, Color.parseColor("#313131"));
        selColor = a.getColor(R.styleable.MenuItem_menu_text_sel_color, Color.parseColor("#ff6b92"));
        a.recycle();
        pic = view.findViewById(R.id.menu_icon);
        title = view.findViewById(R.id.menu_text);
        title.setText(text);
        red=view.findViewById(R.id.tv_red);
        if (noSelRes != 0) {
            ImgUtils.Glide(pic,noSelRes, RequestOptions.diskCacheStrategyOf(DiskCacheStrategy.NONE));
           // Glide.with(context).load(noSelRes).into(pic);
        }else {
            if(selRes!=0){
                ImgUtils.Glide(pic,selRes, RequestOptions.diskCacheStrategyOf(DiskCacheStrategy.NONE));
              //  Glide.with(context).load(selRes).into(pic);
            }
        }
    }

    public SuperTextView showRed(boolean isShow){
        red.setVisibility(isShow?VISIBLE:GONE);
        return red;
    }
    public void setPlaceView(View placeView) {
        this.placeView = placeView;
    }

    @Override
    public void selected(boolean isSelected) {
        setSelected(isSelected);
    }

    @Override
    public void setSelected(boolean selected) {
        super.setSelected(selected);
        if(placeView!=null){
            placeView.setSelected(selected);
        }
        int res;
        if (selected) {
            res = selRes == 0 ? noSelRes : selRes;
        } else {
            res = noSelRes == 0 ? selRes : noSelRes;
        }
        if (res != 0 && pic != null) {
            Glide.with(getContext())
                    .load(res)
                    .apply(RequestOptions.diskCacheStrategyOf(DiskCacheStrategy.NONE))
                    .into(pic);
        }
        if (title != null) {
            title.setTextColor(selected ? selColor : norColor);
        }
        if(selected && isAnim){
            startAnim();
        }
    }
    private void startAnim(){
        ObjectAnimator alphaAnim= ObjectAnimator.ofFloat(pic,"alpha",0.5f,1,1);
        alphaAnim.setDuration(200);
        ObjectAnimator scaleX=ObjectAnimator.ofFloat(pic,"scaleX",0.8f,1.3f,1);
        scaleX.setDuration(500);
        ObjectAnimator scaleY=ObjectAnimator.ofFloat(pic,"scaleY",0.8f,1.3f,1);
        scaleY.setDuration(500);
        AnimatorSet set=new AnimatorSet();
        set.playTogether(alphaAnim,scaleX,scaleY);
        set.start();
    }
}
